package com.jiu345.users.service.impl;

import com.jiu345.users.mapper.RegionMapper;
import com.jiu345.users.pojo.Region;
import com.jiu345.users.service.IRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Objects;

/**
 * @author MrLei QQ:235921049
 * desc:用户地址
 * @Date 2024.07.29 08:17
 */
@RequiredArgsConstructor
@Service
public class IRegionServiceImpl implements IRegionService {
    private final RegionMapper baseMapper;

    /*地区列表*/
    public List<Region> selectList(Region bo) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        //查询所有列表
        List<Region> regions = baseMapper.selectList(wrapper);
        //查询省级列表
        List<Region> provinces = regions.stream().filter(province -> province.getParentCode() == 0).toList();
        //查询市级列表
        provinces.forEach(province -> {
            List<Region> cities = regions.stream()
                .filter(city -> Objects.equals(city.getParentCode(), province.getCode())).toList();
            //封装
            province.setChildren(cities);
            //查询县区级列表
            cities.forEach(district-> {
                List<Region> districts = regions.stream()
                    .filter(area -> Objects.equals(area.getParentCode(), district.getCode())).toList();
                //封装
                district.setChildren(districts);
            });
        });
        //封装地区列表
        return provinces;
    }
}
